package com.llxz.blog.sta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llxz.blog.sta.Client.ArticleClient;
import com.llxz.blog.sta.Client.UcenterClient;
import com.llxz.blog.sta.entity.Sta;
import com.llxz.blog.sta.mapper.StaMapper;
import com.llxz.blog.sta.service.StaService;
import com.llxz.blog.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by IntelliJ IDEA.
 *
 * @author lyq
 * @description: 网站统计日数据 服务实现类
 * date: 2021/9/3 16:11
 */
@Service
public class StaServiceImpl extends ServiceImpl<StaMapper, Sta> implements StaService {

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private ArticleClient articleClient;

    @Autowired
    private StaMapper staMapper;

    @Override
    public void Counts(String day) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<Sta> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
//        staMapper.delete(wrapper);
        if(wrapper!=null){
            System.out.println("不为空");
        }else{
            R r = ucenterClient.countRegister(day);
            int articleSum = articleClient.getEveryDayArticles(day);
            Integer countRegister = (Integer)r.getData().get("countRegister");
            //把获取数据添加数据库，统计分析表里面
            Sta sta = new Sta();
            sta.setRegisterNum(countRegister); //注册人数
            sta.setDateCalculated(day);//统计日期
            sta.setArticleNum(articleSum);//文章数
            staMapper.insert(sta);
        }
    }

    @Override
    public Map<String, Object> getShowData(String begin,String end) {
        //根据条件查询对应数据
        QueryWrapper<Sta> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated","register_num");//指定查询字段
        List<Sta> staList = staMapper.selectList(wrapper);
        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculatedList1 = new ArrayList<>();
        List<Integer> numDataList1 = new ArrayList<>();
        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            Sta daily = staList.get(i);
            //封装日期list集合
            date_calculatedList1.add(daily.getDateCalculated());
            numDataList1.add(daily.getRegisterNum());
        }
        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("dateList",date_calculatedList1);
        map.put("registerNumList",numDataList1);
//      文章总数
        QueryWrapper<Sta> wrapper1 = new QueryWrapper<>();
        wrapper1.between("date_calculated",begin,end);
        wrapper1.select("date_calculated","article_num");//指定查询字段
        List<Sta> staList2 = staMapper.selectList(wrapper1);
        List<Integer> numDataList2 = new ArrayList<>();
        for (int i = 0; i < staList2.size(); i++) {
            Sta daily = staList2.get(i);
            //封装对应数量
            numDataList2.add(daily.getArticleNum());
        }
        map.put("articleNumList",numDataList2);
        return map;
    }

    @Override
    public void AutoCount(String formatDate) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<Sta> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",formatDate);
        staMapper.delete(wrapper);
        R r = ucenterClient.countRegister(formatDate);
        int articleSum = articleClient.getEveryDayArticles(formatDate);
        Integer countRegister = (Integer)r.getData().get("countRegister");
        //把获取数据添加数据库，统计分析表里面
        Sta sta = new Sta();
        sta.setRegisterNum(countRegister); //注册人数
        sta.setDateCalculated(formatDate);//统计日期
        sta.setArticleNum(articleSum);//点击人数
    }

}

